<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName2" type="card">
          <el-row style="height: 60px">
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="hAdd"
            >新增角色</el-button>
          </el-row>

          <el-tab-pane label="用户管理" name="first">
            <el-table :data="lores">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :page-size="pageParams.pagesize"
                layout="prev,pager,next"
                :total="total"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <!-- :title="isEdit?'编辑':'新增'" -->
      <el-dialog
        :title="cTitle"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialogAssign"
      >
        <!-- @close="hClose" -->
        <AssignPermission
          v-if="showDialogAssign"
          :role-id="roleId"
          @close="showDialogAssign=false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, deleteRole, addRole, updateRole } from '@/api/setting'
import AssignPermission from './assignPermission'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      roleId: '',
      showDialog: false,
      showDialogAssign: false, // 控制权限分配
      roleForm: {
        name: '',
        description: ''
      },
      isEdit: false,
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      activeName2: 'first',
      lores: [],
      pageParams: {
        page: 1,
        pagesize: 2
      },
      total: 0
    }
  },
  computed: {
    cTitle() {
      return this.isEdit ? '编辑' : '新增'
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log('获取角色信息', res)
        this.lores = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    // 点击页码
    hCurrentChange(curPage) {
      // console.log(curPage)
      this.pageParams.page = curPage
      this.loadRoles()
    },
    async doDel(id) {
      try {
        const res = await deleteRole(id)
        console.log('删除', res)
        this.$message.success('删除成功')
        // 如果没有数据自动请求前一页数据
        if (this.lores.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        this.loadRoles()
      } catch (err) {
        console.log(err)
        this.$message.error('删除成功')
      }
    },
    hDel(id) {
      console.log('删除的id', id)
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 1. 去做删除
        this.doDel(id)
        // // 3. 更新页面- 重发请求
        // this.loadRoles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hSubmit() {
      // 手动兜底
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async doAdd() {
      try {
        const res = await addRole(this.roleForm)
        console.log(res)
        this.loadRoles()
        this.$message.success('添加成功')
        this.showDialog = false
      } catch (err) {
        this.$message.error('添加失败')
        console.log(err)
      }
    },
    // 用户点击了编辑
    hEdit(row) {
      // this.roleForm = row
      this.roleForm = { id: row.id, name: row.name, description: row.description }
      this.showDialog = true
      this.isEdit = true
    },
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    async doEdit() {
      try {
        const res = await updateRole(this.roleForm)
        console.log(res)
        this.loadRoles()
        this.$message.success('编辑成功')
        this.showDialog = false
      } catch (err) {
        this.$message.error('编辑失败')
        console.log(err)
      }
    },
    hClose() {
      console.log('close................')
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    hAssign(id) {
      this.roleId = id
      this.showDialogAssign = true
    }
  }
}
</script>
